//
//  NSString+Timestamp.m
//  YBInsurance
//
//  Created by S2 on 6/28/16.
//  Copyright © 2016 YB. All rights reserved.
//

#import "NSString+Timestamp.h"

@implementation NSString (Timestamp)

+ (NSString *)timestamp {
    
    NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];
    long long dTime = [[NSNumber numberWithDouble:timestamp] longLongValue];
    NSString *time = [NSString stringWithFormat:@"%lld", dTime];
    return time;
}

+ (NSString *)futureTimestamp {
    
    NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];
    long long dTime = [[NSNumber numberWithDouble:timestamp] longLongValue] + 60;
    NSString *time = [NSString stringWithFormat:@"%lld", dTime];
    return time;
}

+ (NSString *)timestampAtDate:(NSDate *)date {
    
    NSTimeInterval timestamp = [date timeIntervalSince1970];
    long long dTime = [[NSNumber numberWithDouble:timestamp] longLongValue];
    NSString *time = [NSString stringWithFormat:@"%lld", dTime];
    return time;
}

- (NSString *)timeChangeToThirteen {
    
    if (self.length == 13) {
        
        return self;
    }
    
    NSString *newTime = [NSString stringWithFormat:@"%@000", self];
    return newTime;
}

- (NSString *)timestampConvertToTen {
    
    if ([self isThatNull] || self == nil || [self isEqualToString:@"<null>"]) {
        
        return @"";
    }
    
    if (self.length <= 10) {
        
        return self;
    }
    
    NSTimeInterval timestamp = [[self substringToIndex:10] doubleValue];
    long long dTime = [[NSNumber numberWithDouble:timestamp] longLongValue];
    NSString *time = [NSString stringWithFormat:@"%lld", dTime];
    return time;
}

+ (NSString *)yb_stringDateForToday {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    return dateString;
}

- (NSString *)yb_stringDateCompareWithStandardDate:(NSDate *)standardDate {
    
    NSDate *nDate = [NSDate dateWithTimeIntervalSince1970:self.longLongValue];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *nComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nDate];
    NSDateComponents *sComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:standardDate];
    
    if (nComponents.year == sComponents.year && nComponents.month == sComponents.month && nComponents.day == sComponents.day) {
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        NSString *finalDate = [dateFormatter stringFromDate:nDate];
        return finalDate;
    } else if (nComponents.year == sComponents.year && nComponents.month == sComponents.month && (sComponents.day - nComponents.day == 1)) {
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        NSString *finalDate = [NSString stringWithFormat:@"昨天 %@", [dateFormatter stringFromDate:nDate]];
        return finalDate;
    } else {
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm"];
        NSString *finalDate = [dateFormatter stringFromDate:nDate];
        return finalDate;
    }
    return @"";
}

- (NSDate *)yb_stringDateConvertToDate {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *inputDate = [dateFormatter dateFromString:self];
    return inputDate;
}

- (NSString *)yb_stringDateConvertToYYYYMMdd {
    
    if ([self isThatNull]) {
        
        return @"-";
    }
    
    if (![self containsString:@" "]) {
        
        return self;
    } else {
        
        return [self componentsSeparatedByString:@" "].firstObject;
    }
}

- (NSString *)yb_stringDateConvertToYYYYMMddHH {
    
    if ([self isThatNull]) {
        
        return @"-";
    }
    
    if (![self containsString:@" "]) {
        
        return self;
    } else {
        
        NSArray *dateArray = [self componentsSeparatedByString:@" "];
        NSString *ymd = dateArray.firstObject;
        NSArray *hmsArray = [dateArray.lastObject componentsSeparatedByString:@":"];
        return [NSString stringWithFormat:@"%@ %@:%@", ymd, hmsArray[0], hmsArray[1]];
    }
}

///时间戳转化为字符转0000-00-00 00:00:00

+ (NSString *)time_timestampToString:(NSInteger)timestamp{
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString* string=[dateFormat stringFromDate:confromTimesp];
    
    return string;
    
}

@end
